//
//  WKMainTabBar.swift
//  WKWeibo
//
//  Created by 恋guang年 on 16/3/25.
//  Copyright © 2016年 恋guang年. All rights reserved.
//
/// 懒加载加号按钮 在layoutsubviews里重新布局
import UIKit
let tabBarItemCount = 5
class WKMainTabBar: UITabBar {
    //此方法调用了两次 UITabBar Subviews 包含 一个UITabBarBackgroundView 一个UIImageView
    override func layoutSubviews() {
        super.layoutSubviews()
        //每个tabbarItem的宽
        let width = frame.width / CGFloat(tabBarItemCount)
        var index = 0
        for view in subviews {
        
            //如果是UITabBarButton类型就重新布局
            if view.isKindOfClass(NSClassFromString("UITabBarButton")!){
                view.frame = CGRect(x: CGFloat(index)*width, y: 0, width: width, height: frame.height)
                index += index == 1 ? 2 : 1
            }
            //添加加号按钮的frame 
            addButton.frame = CGRect(x: width * 2, y: 0, width: width, height: frame.height)
            
        }
    }
    
    
  //MARK: - 懒加载加号按钮
    private lazy var addButton:UIButton = {
        let button:UIButton = UIButton()
       //设置背景图
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //添加到tabbar上
        self.addSubview(button)
        return button
    }()
 
}
